﻿Imports SpatialAlbum.WPF.EnumPage

Partial Public Class MainPage

    Private Sub MainPage_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        'Frame1.Navigate(PageManager.Page(eIntroduction))
        Frame1.Navigate(PageManager.Page(ePhotos))
        AddHandler g_imageloader.StartLoadingImage, AddressOf StartLoadingImage
        AddHandler g_imageloader.ImageLoaded, AddressOf ImageLoaded
        AddHandler g_imageloader.FinishedLoadingImages, AddressOf FinishedLoadingImages
    End Sub

    Private Delegate Sub DlgJobEvent(ByVal job As ImageJob)
    Private Sub StartLoadingImage(ByVal job As ImageJob)
        If Not Me.Dispatcher.CheckAccess() Then
            Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Normal, New DlgJobEvent(AddressOf StartLoadingImage), job)
        Else
            Dim done As Integer = g_imageloader.DoneCount
            Dim queued As Integer = g_imageloader.QueueCount
            SetStatusbarMessage("loading photo " & (done + 1) & " of " & (queued + done + 1))
        End If
    End Sub

    Private Sub ImageLoaded(ByVal job As ImageJob)
        If Not Me.Dispatcher.CheckAccess() Then
            Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Normal, New DlgJobEvent(AddressOf ImageLoaded), job)
        Else
            Dim done As Integer = g_imageloader.DoneCount
            Dim queued As Integer = g_imageloader.QueueCount
            If queued = 0 And done = 0 Then
                SetStatusbarMessage("")
            Else
                SetStatusbarMessage("loaded photo " & (done) & " of " & (queued + done))
            End If
        End If
    End Sub

    Private Delegate Sub DlgFinishedLoadingImages(ByVal njobs As Integer)
    Private Sub FinishedLoadingImages(ByVal njobs As Integer)
        If Not Me.Dispatcher.CheckAccess() Then
            Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Normal, New DlgFinishedLoadingImages(AddressOf FinishedLoadingImages), njobs)
        Else
            SetStatusbarMessage("loaded " & njobs & " photos")
        End If
    End Sub

    Private Delegate Sub DlgSetStatusbarMessage(ByVal msg As String)
    Public Sub SetStatusbarMessage(ByVal msg As String)
        If Not Me.Dispatcher.CheckAccess() Then
            Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Normal, New DlgSetStatusbarMessage(AddressOf SetStatusbarMessage), msg)
        Else
            txtStatus.Text = msg
        End If
    End Sub

    Public Delegate Sub DlgSetStatusbarError(ByVal msg As String)
    Public Sub SetStatusbarError(ByVal msg As String)
        If Not Me.Dispatcher.CheckAccess() Then
            Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Normal, New DlgSetStatusbarError(AddressOf SetStatusbarError), msg)
        Else
            txtStatus.Text = msg
            txtStatus.Background = System.Windows.Media.Brushes.Red
        End If
    End Sub

    Public Delegate Sub DlgClearStatusbar()
    Public Sub ClearStatusbar()
        If Not Me.Dispatcher.CheckAccess() Then
            Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Normal, New DlgClearStatusbar(AddressOf ClearStatusbar))
        Else
            txtStatus.Text = ""
            txtStatus.Background = Nothing
        End If
    End Sub

    Private Sub SetTitle(ByVal title As String)
        g_mainwindow.Title = My.Application.Info.Title & " " & My.Application.Info.Version.ToString & " - " & title
    End Sub

    Public Sub ShowPage(ByVal pagetype As EnumPage)
        Frame1.Navigate(PageManager.Page(pagetype))
    End Sub

    Private Sub Frame1_Navigated(ByVal sender As Object, ByVal e As System.Windows.Navigation.NavigationEventArgs) Handles Frame1.Navigated
        If e.Content Is Nothing Then Return
        Dim title As String = e.Content.Title
        If title = "" Then Return
        SetTitle(title)
    End Sub

    Private Sub btnConnection_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnConnection.Click
        ShowPage(eConnection)
    End Sub

    Private Sub btnDatabaseTools_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnDatabaseTools.Click
        ShowPage(eDatabaseTools)
    End Sub

    Private Sub btnImageLoader_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnImageLoader.Click
        ShowPage(eImageLoader)
    End Sub

    Private Sub btnIntroduction_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnIntroduction.Click
        ShowPage(eIntroduction)
    End Sub

    Private Sub btnLog_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnLog.Click
        ShowPage(eLog)
    End Sub

    Private Sub btnOptions_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnOptions.Click
        ShowPage(eOptions)
    End Sub

    Private Sub btnPhotos_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnPhotos.Click
        ShowPage(ePhotos)
    End Sub

    Private Sub btnAlbums_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAlbums.Click
        ShowPage(eAlbums)
    End Sub

    Private Sub btnSqlInspector_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSqlInspector.Click
        ShowPage(eSqlInspector)
    End Sub

End Class
